package mspbots.qbd.desktop.core;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * mspbots.data.quickbooks.ws.LoadQbXmlService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/5/28
 */
@Log4j2
@Service
public class RequestXmlService {

    public String requestXml(SoapSession session) {
        StringBuilder stringBuffer = new StringBuilder("<?xml version=\"1.0\" ?>\n");
        stringBuffer.append("<?qbxml version=\"13.0\"?>\n");
        stringBuffer.append("<QBXML>\n<QBXMLMsgsRq onError=\"continueOnError\">\n");

        if (session.getIndex() == RequestDataType.ACCOUNT) {
            stringBuffer.append(this.requestAccount(session));
        } else if (session.getIndex() == RequestDataType.CUSTOMER) {
            stringBuffer.append(this.requestCustomer(session));
        } else if (session.getIndex() == RequestDataType.PROFIT_AND_LOSS_STANDARD) {
            stringBuffer.append(this.requestProfitAndLossStandardXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.PROFIT_AND_LOSS_DETAIL) {
            stringBuffer.append(this.requestProfitAndLossDetailXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.BALANCE_SHEET_SUMMARY) {
            stringBuffer.append(this.requestBalanceSheetSummaryXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.BALANCE_SHEET_DETAIL) {
            stringBuffer.append(this.requestBalanceSheetDetailXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.PROFIT_AND_LOSS_PREV_YEAR_COMP) {
            stringBuffer.append(this.requestPrevYearXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.PROFIT_AND_LOSS_YTD_COMP) {
            stringBuffer.append(this.requestYtdXml(session.getStartDate()));
        } else if (session.getIndex() == RequestDataType.INVOICE) {
            stringBuffer.append(this.requestInvoice(session));
        } else if (session.getIndex() == RequestDataType.BILL) {
            stringBuffer.append(this.requestBill(session));
        } else if (session.getIndex() == RequestDataType.JOURNAL) {
            stringBuffer.append(this.requestJournal(session));
        } else {
            stringBuffer.append("<AccountQueryRq><ActiveStatus>All</ActiveStatus></AccountQueryRq>\n");
        }
        stringBuffer.append("</QBXMLMsgsRq>\n</QBXML>\n");
        return stringBuffer.toString();
    }

    /**
     * "<AccountQueryRq>\n<ActiveStatus>All</ActiveStatus>\n").append("</AccountQueryRq>\n"
     *
     * @return xml count
     */
    private String requestAccount(SoapSession session) {
        return "<AccountQueryRq>\n" +
                "   <ActiveStatus>All</ActiveStatus>\n" +
                "</AccountQueryRq>\n";
    }

    private String requestCustomer(SoapSession session) {
        return "<CustomerQueryRq " + session.getIterator() + ">\n" +
                "   <MaxReturned>" + session.getSize() + "</MaxReturned>\n" +
                "   <ActiveStatus>All</ActiveStatus>\n" +
                "</CustomerQueryRq>\n";
    }

    private String requestInvoice(SoapSession session) {
        return "<InvoiceQueryRq " + session.getIterator() + ">\n" +
                "   <MaxReturned>" + session.getSize() + "</MaxReturned>\n" +
//                    "<ModifiedDateRangeFilter>"+
//                        " <FromModifiedDate>" +   session.getStartDate().with(TemporalAdjusters.firstDayOfYear()) + "</FromModifiedDate> \n"+
//                        " <ToModifiedDate>" + session.getStartDate().plusYears(2).with(TemporalAdjusters.lastDayOfYear()) + "</ToModifiedDate>"+
//                    "</ModifiedDateRangeFilter>\n"+
                "</InvoiceQueryRq>\n";
    }


    private String requestJournal(SoapSession session) {
        return "<JournalEntryQueryRq " + session.getIterator() + ">\n" +
                "   <MaxReturned>" + session.getSize() + "</MaxReturned>\n" +
//                    "<ModifiedDateRangeFilter>"+
//                        "<FromModifiedDate >"+session.getStartDate().with(TemporalAdjusters.firstDayOfYear())+"</FromModifiedDate>"+
//                        "<ToModifiedDate >"+session.getStartDate().plusYears(2).with(TemporalAdjusters.lastDayOfYear())+"</ToModifiedDate>"+
//                    "</ModifiedDateRangeFilter>"+
                "</JournalEntryQueryRq>\n";
    }


    private String requestBill(SoapSession session) {
        return "<BillQueryRq " + session.getIterator() + ">\n" +
                "   <MaxReturned>" + session.getSize() + "</MaxReturned>\n" +
//                    "<ModifiedDateRangeFilter>"+
//                        "<FromModifiedDate >"+session.getStartDate().with(TemporalAdjusters.firstDayOfYear())+"</FromModifiedDate>"+
//                        "<ToModifiedDate >"+session.getStartDate().plusYears(2).with(TemporalAdjusters.lastDayOfYear())+"</ToModifiedDate>"+
//                    "</ModifiedDateRangeFilter>"+
                "</BillQueryRq>\n";
    }

    private String requestYtdXml(LocalDate startDate) {
        return "<GeneralSummaryReportQueryRq>\n" +
                "<GeneralSummaryReportType>ProfitAndLossYTDComp</GeneralSummaryReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralSummaryReportQueryRq>\n";
    }

    private String requestPrevYearXml(LocalDate startDate) {
        return "<GeneralSummaryReportQueryRq>\n" +
                "<GeneralSummaryReportType>ProfitAndLossPrevYearComp</GeneralSummaryReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralSummaryReportQueryRq>\n";
    }

    private String requestBalanceSheetSummaryXml(LocalDate startDate) {
        return "<GeneralSummaryReportQueryRq>\n" +
                "<GeneralSummaryReportType>BalanceSheetSummary</GeneralSummaryReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralSummaryReportQueryRq>\n";
    }

    private String requestBalanceSheetDetailXml(LocalDate startDate) {
        return "<GeneralDetailReportQueryRq>\n" +
                "<GeneralDetailReportType>BalanceSheetDetail</GeneralDetailReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralDetailReportQueryRq>\n";
    }

    private String requestProfitAndLossStandardXml(LocalDate startDate) {
        return "<GeneralSummaryReportQueryRq>\n" +
                "<GeneralSummaryReportType>ProfitAndLossStandard</GeneralSummaryReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralSummaryReportQueryRq>\n";
    }

    private String requestProfitAndLossDetailXml(LocalDate startDate) {
        return "<GeneralDetailReportQueryRq>\n" +
                "<GeneralDetailReportType>ProfitAndLossDetail</GeneralDetailReportType>\n" +
                "<ReportPeriod>\n<FromReportDate>" +
                startDate.with(TemporalAdjusters.firstDayOfMonth()) +
                "</FromReportDate>\n" +
                "<ToReportDate>" +
                startDate.with(TemporalAdjusters.lastDayOfMonth()) +
                "</ToReportDate>\n" +
                "</ReportPeriod>\n" +
                "</GeneralDetailReportQueryRq>\n";
    }

}
